package com.sdp.wx.controller;


import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_wz_procured_detail.entity.WxWzProcuredDetail;
import com.sdp.wx.db.wx_wz_procured_detail.service.IWxWzProcuredDetailService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-01-20
 */
@RestController
@RequestMapping("/WxWzProcuredDetailController")
public class WxWzProcuredDetailController {

	@Autowired
	private IWxWzProcuredDetailService iWxWzProcuredDetailService;
	
	@Autowired
	private Query query;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxWzProcuredDetailService, WxWzProcuredDetail.class, dataCenter));
	}
	
	@PostMapping(value = "/list")
	public Result<?> list(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.list(iWxWzProcuredDetailService, WxWzProcuredDetail.class, dataCenter));
	}
	
	/**
	 * 添加采购的零配件
	 * @param items
	 * @return
	 */
	@PostMapping(value = "/addWz")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> addItem(@RequestBody JSONObject obj) {
		if(obj==null){
			return Result.error("器材信息不存在");
		}
		List<WxWzProcuredDetail> items = JSONArray.parseArray(JSON.toJSONString(obj.get("wzList")), WxWzProcuredDetail.class);
		String planCode = obj.getString("planCode");
		String planId = obj.getString("planId");
		if(StringUtils.isEmpty(planCode)) {
			return Result.error("采购计划编号不存在");
		}
		if(StringUtils.isEmpty(planId)) {
			return Result.error("采购计划信息不存在");
		}
		
		if(items==null || items.size()==0) {
			return Result.error("器材信息不存在");
		}
		for(WxWzProcuredDetail item : items) {
			item.setKeyId(null);
			item.setPlanCode(planCode);
			item.setPlanId(planId);
			item.setCreateTime(LocalDateTime.now());
			item.setProcuredNum(BigDecimal.ONE);
			item.setLowestPrice(BigDecimal.ZERO);
			item.setArrivalNum(BigDecimal.ZERO);
			item.setNotArrivalNum(BigDecimal.ONE);
			
			
		}
		List<WxWzProcuredDetail> dbItems = iWxWzProcuredDetailService.queryWxWzProcuredDetailByPlanCode(planCode);
		if(dbItems==null || dbItems.size()==0) {
			iWxWzProcuredDetailService.saveBatch(items);
		}else {
			Iterator<WxWzProcuredDetail> iterator = items.iterator();
			for(WxWzProcuredDetail dbItem : dbItems) {
				if(items.size()==0) {
					break;
				}
				iterator = items.iterator();
				while (iterator.hasNext()) {
					if(iterator.next().getWzNo().equals(dbItem.getWzNo())) {
						iterator.remove();
						break;
					}
				}
			}
			
			if(items.size()>0) {
				iWxWzProcuredDetailService.saveBatch(items);
			}
		}
		return Result.ok();
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxWzProcuredDetail wxWzProcuredDetail) {
		if(wxWzProcuredDetail==null) {
			return Result.error("器材信息不存在");
		}
		//设置未到货数量，未到货数量就是采购数量
		wxWzProcuredDetail.setNotArrivalNum(wxWzProcuredDetail.getProcuredNum());
		iWxWzProcuredDetailService.updateById(wxWzProcuredDetail);
		return Result.ok();
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId) {
		if(StringUtils.isEmpty(keyId)) {
			return Result.error("需要删除的记录信息不完整");
		}
		iWxWzProcuredDetailService.removeById(keyId);
		return Result.ok();
	}
	
	/**
	 * 根据采购单编号，查询满足到货条件的器材信息
	 * @param planCode
	 * @return
	 */
	@GetMapping(value = "/queryCanArrivalWzProcuredDetailByPlanCode")
	public Result<?> queryWxWzProcuredDetailyPlanCode(@RequestParam(name="planCode",required=true) String planCode) {
		if(StringUtils.isEmpty(planCode)) {
			return Result.error("采购单编号为空");
		}
		List<WxWzProcuredDetail> li = iWxWzProcuredDetailService.queryCanArrivalWzProcuredDetailByPlanCode(planCode);
		if(li==null || li.size()==0) {
			return Result.error("此采购单没有未到货的器材，请确认");
		}
		return Result.ok(li);
	}
}

